Skip backtrace capture on internal validation exceptions#2687
Merged
ericproulx merged 1 commit intomasterfrom Apr 20, 2026
Merged
Skip backtrace capture on internal validation exceptions#2687ericproulx merged 1 commit intomasterfrom
ericproulx merged 1 commit intomasterfrom
Conversation
1c18146 to
44fe03a
Compare
Danger ReportNo issues found. |
44fe03a to
e527b71
Compare
dblock
approved these changes
Apr 19, 2026
Grape::Exceptions::Validation is raised once per bad attribute on every failing request; Grape::Exceptions::ValidationArrayErrors wraps them and is raised once per validator. Both are caught internally by Endpoint#run_validators and their backtraces are never surfaced — they point into Grape's own validator stack, not user code, and have no diagnostic value. Ruby's +raise+ only captures a backtrace if the exception's backtrace ivar is nil. Pre-seeding it via +set_backtrace(EMPTY_BACKTRACE)+ in +initialize+ makes +raise+ skip the C-level +make_backtrace+ call entirely. At representative prod stack depth (80-150 frames) this saves ~1-2 µs per raise. The outer Grape::Exceptions::ValidationErrors is untouched — its backtrace is preserved for anyone doing +rescue_from ValidationErrors+ or shipping validation failures to error-reporting tools. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
e527b71 to
c176526
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Grape::Exceptions::Validationis raised once per bad attribute on every failing request;Grape::Exceptions::ValidationArrayErrorswraps them and is raised once per validator. Both are caught internally byEndpoint#run_validatorsand their backtraces are never surfaced — they point into Grape's own validator stack, not user code, and have no diagnostic value.Ruby's
raiseonly captures a backtrace if the exception's backtrace ivar isnil. Pre-seeding it viaset_backtrace(EMPTY_BACKTRACE)ininitializemakesraiseskip the C-levelmake_backtracecall entirely.Backward compatibility
The outer
Grape::Exceptions::ValidationErrorsis untouched — its backtrace is preserved for anyone doingrescue_from ValidationErrorsor shipping validation failures to error-reporting tools (Sentry, Rollbar, etc.). Only the two inner exceptions — which are private to the validator internals — have their backtrace capture skipped.Perf
Measured on a synthetic benchmark at stack depth 40:
Savings scale linearly with stack depth since
make_backtraceisO(frames). At representative prod stack depth (Rack + middleware chain + Grape + validators, typically 80-150 frames) this is closer to ~1-2 µs per raise.A request failing 5 attributes across 2-3 validators raises ~7-8 inner exceptions → ~7-15 µs per failing request. Failure paths on public APIs get hammered by bots and misconfigured clients, so at scale this adds up.
Test plan
🤖 Generated with Claude Code